<template>
  <div class="main">
    <img src="@/assets/search.png" alt="" :class="{active:flag}" class="searchIcon">
    <input class="search" ref="search" v-model="keyword" background="#fff" 
    placeholder="请输入关键词搜索"  @focus="handleFouce" @blur="handleBlur" @keyup.enter="handleSearch"/>

    <van-swipe class="my-swipe" :autoplay="3000" style="width: 90%;margin:auto;margin-top: 50px;;"
    indicator-color="white"  @change="onChange">
        <van-swipe-item v-for="item in imgList" :key="item.id">
            <img v-lazy="item.image" alt="">
        </van-swipe-item>
        <template #indicator>
            <div class="custom-indicator">
                <span>{{ current + 1 }}</span>/
                <span>{{ imgList.length }}</span>
            </div>
        </template>
    </van-swipe>
    <!-- <van-row type="flex" justify="space-around" style="height: 98px;background-color: #fff;margin: 0 auto;">
        <van-row type="flex" align="center" justify="space-around"
        v-for="item in cateList" :key="item.id">
            <van-col span="16" style="font-size:14px;padding-left:15px">
                <img v-lazy="item.image" style="width:40px;height: 40px;">
                {{ item.description }}
            </van-col>
        </van-row>
    </van-row> -->
    <van-grid :border="false">
      <van-grid-item v-for="item in cateList" :key="item.id" style="font-size:14px;" :to="{path:'/product/list',query:{cat_id:item.target}}">
        <van-image :src="item.image" width="40px" height="40px" />
        {{ item.description }}
      </van-grid-item>
    </van-grid>
    <img :src="ad.image" alt="" style="width: 100%;height: 100%;">
    <van-row type="flex" justify="space-around" align="center" style="width: 100%;height:60px;margin: 10px auto;">
        <van-col span="4" style="background-color: #fff;height:60px;padding-top: 10px;padding-left: 20px;">
          <img src="@/assets/diamond.png" alt="" style="width: 40px; height: 40px;">
        </van-col>
        <van-col span="20" style="background-color: #fff;height:60px;padding-top: 8px;">
          <van-cell title="分类精选" is-link to="/category"  size="large"/>
        </van-col>
    </van-row>
    
    <div class="goods" v-for="item in prodList" :key="item.id">
      <p class="title">{{item.title}}</p>
      <ul>
        <li v-for="sub in item.products" :key="sub.id" @click="handleProdOne(sub.id)">
          <img :src="sub.image[0]" alt="">
          <p>{{ sub.description }}</p>
          <span>￥{{ sub.price }}</span>
        </li>
        <li class="more" @click="handleMore(item.products)">
            <p>查看更多</p>
            <p>More+</p>
        </li>
      </ul>
    </div>
  </div>
</template>
<script>
import {getNav,getHomeProducts} from "@/api/home"
export default {
  data() {
    return {
        keyword:"",
        flag:false,
        current:0,
        imgList:[],
        cateList:[],
        ad:{},
        prodList:[],
    };
  },
  created() {
    getNav().then(res=>{
        this.imgList = res.data.data[0];
        this.cateList = res.data.data[1];
        this.ad = res.data.data[2][0]
    })
    getHomeProducts().then(res=>{
      this.prodList = res.data.data.home_recommend_products;
      console.log(this.prodList);
    })
  },
  mounted(){
    this.$refs.search.style.textAlign = "center"
  },
  computed: {},
  methods: {
    onChange(index) {
      this.current = index;
    },
    handleBlur(){
        this.$refs.search.style.textAlign = "center"
        this.flag = false;
    },
    handleFouce(){
        this.$refs.search.style.textAlign = "left"
        this.flag = true;
    },
    handleMore(item){
      this.$router.push({path:"/product/list",query:{cat_id:item[0].category_id}})
    },
    handleProdOne(id){
        this.$router.push({path:"/product",query:{id:id,page:1}})
    },
    handleSearch(){
      this.$router.push({path:"/product/list",query:{kw:this.keyword}})
    }
  },
};
</script>
<style scoped>
    .main{
        background-color: #F5F5F5;
        overflow: hidden;
    }
    .search{
        width: 85%;
        height: 30px;
        position: absolute;
        left:50%;
        transform: translate(-50%,0);
        border-radius: 15px;
        background-color: #EBEBEB;
        border: none;
        margin: 10px 0;
        padding-left: 10%;
        z-index: 1;
    }
    .searchIcon{
        width: 20px;
        height: 20px;
        position: absolute;
        left:35%;
        top: 15px;
        transform: translate(-50%,0);
        z-index: 2;
    }
    .active{
        position: absolute;
        left:10%;
    }

    .my-swipe .van-swipe-item {
        color: #fff;
        font-size: 20px;
        line-height: 150px;
        text-align: center;
        height: 175px;
    }
    .my-swipe .van-swipe-item img{
        width: 100%;
        height: 100%;
        position: absolute;
        left: 50%;
        transform: translate(-50%,0);
    }
  .custom-indicator {
    position: absolute;
    left: 5px;
    bottom: 5px;
    padding: 2px 5px;
    font-size: 12px;
    color: #fff;
    display: flex;
    justify-content: space-around;
    align-items: center;
  }
  .custom-indicator  span{
    display: inline-block;
    width: 20px;
    height: 20px;
    border-radius: 10px;
    background: rgba(0, 0, 0, 0.5);
    line-height: 20px;
    text-align: center;
  }

  .goods{
    width: 100%;
    height: 200px;
    background-color: #fff;
  }
  .title{
    width: 100%;
    height: 45px;
    border-left: 4px solid #FC7592;
    line-height: 45px;
    text-indent: 1em;
  }
  .goods {
    margin-bottom: 30px;
    overflow: auto;
  }
  .goods::-webkit-scrollbar{
    display: none;
  }
  .goods ul{
    display: flex;
    width: 100%;
    justify-content: flex-start;
    align-items: center;
    overflow:auto hidden;
    /* padding-left: 150px; */
  }
  .goods ul li img{
    width: 90px;
    height: 90px;
  }
  .goods ul li {
    width: 90px;
    height: 155px;
    font-size: 13px;
    margin:0 10px;
  }
  .goods ul li p{
    width: 90px;
    height: 15px;
    line-height: 15px;
    overflow: hidden;
    white-space:nowrap;
    text-overflow: ellipsis;
    color: #303133;
    margin: 5px 0;
  }
  .goods ul li span{
    color: #fa436a;
  }
  .goods ul .more{
    width: 90px;
    height: 90px;
    background-color: #f3f3f3;
    color: #909399;
    font-size: 14px;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: center;
    border-radius: 5px;
    cursor: pointer;
    margin-top: -60px;
  }
  .goods ul .more p{
    width: 90px;
    height: 45px;
    text-align: center;
    line-height: 45px;
  }
</style>
